package client;

import java.awt.Graphics;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Socket;

import javax.swing.JOptionPane;
import javax.swing.plaf.metal.MetalIconFactory;

import util.MyParser;

public class ReceiveThreadClient extends Thread {
	private Socket socket;

	private InputStream is;
	private BufferedReader in;
	private GuiListener gl;

	public ReceiveThreadClient(Socket socket, GuiListener gl) {
		this.socket = socket;
		this.gl = gl;
		try {
			is = socket.getInputStream();
			in = new BufferedReader(new InputStreamReader(is));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void run() {
		String line = " ";

		while (line != null) {
			try {
				line = in.readLine();
				if (line != null) {
					checkType(line);
				}
			} catch (IOException e) {
				e.printStackTrace();
			}

		}
	}

	private void checkType(String s) {
		if (MyParser.isPaint(s)) {

			String[] v = s.split(MyParser.FIELD);
			GUI.setColor(v[2]);

			GUI.drawList(MyParser.stringToList(v[3]));

		}
		if (MyParser.isChat(s)) {
			String chatt = parseToChat(s);
			GUI.chatHistory.append(chatt + "\n");
			GUI.chatHistory.setCaretPosition(GUI.chatHistory.getDocument()
					.getLength());
		}
		if (MyParser.isGame(s)) {
			String cmd = MyParser.getMessage(s);
			if (cmd.equals("clear")) {
				Graphics g = GUI.sketchArea.getGraphics();
				g.setColor(GUI.sketchArea.getBackground());
				g.fillRect(5, 5, GUI.sketchArea.getWidth() - 10, GUI.sketchArea
						.getHeight() - 10);
				gl.clearPointlist();
			}
			if (cmd.equals("paintmaster")) {
				GUI.setPaintMaster(true);
			}
			if (cmd.equals("notpaintmaster")) {
				GUI.setPaintMaster(false);
				GUI.setWord("");
			}
			if (cmd.equals("time")) {
				GUI.startTime(Integer.parseInt(s.split(MyParser.FIELD)[3]) - 1);
			}
			if (cmd.equals("games")) {
				String[] v = s.split(MyParser.FIELD);
				String[] games = v[3].split(MyParser.DATA);
				gl.setGames(games);
			}
			if (cmd.equals("scores")) {
				String[] v = s.split(MyParser.FIELD);
				String[] points = v[3].split(MyParser.DATA);
				gl.setpoints(points);
			}
			if (cmd.equals("word")) {
				GUI.setWord(s.split(MyParser.FIELD)[3]);
			}
			if (cmd.equals("name")) {
				String username = (String) JOptionPane
						.showInputDialog(
								GUI.sketchArea,
								"Username already in use. Please enter another username",
								"Name", JOptionPane.QUESTION_MESSAGE,
								MetalIconFactory.getTreeComputerIcon(), null,
								"");
				GUI.setUsername(username);
				gl.sendGame("name" + MyParser.FIELD + username);

			}
			if (cmd.equals("endofsession")) {
				GUI.stopTime();
				Graphics g = GUI.sketchArea.getGraphics();
				g.setColor(GUI.sketchArea.getBackground());
				g.fillRect(5, 5, GUI.sketchArea.getWidth() - 10, GUI.sketchArea
						.getHeight() - 10);
				gl.clearPointlist();
			}

		}
	}

	private String parseToChat(String s) {
		StringBuilder sb = new StringBuilder();
		sb.append(MyParser.getName(s) + ":\n");
		s = MyParser.getMessage(s);
		int breaks = s.length() / 25;
		int rest = s.length() % 25;
		if (breaks != 0) {
			for (int i = 0; i < breaks; i++) {
				sb.append(s.substring(i * 25, ((i + 1) * 25) - 1) + "\n");

			}
			sb.append(s.substring(s.length() - (rest + 1), s.length()));

		} else {
			sb.append(s);
		}

		return sb.toString();
	}
}
